using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;

using NBear.Net.Remoting;

namespace NBear.IoC.Service.Configuration
{
    public class ServiceFactorySetting : System.Configuration.IConfigurationSectionHandler
    {
        private string GetAttributeValue(XmlNode node, string attrName)
        {
            if (node.Attributes[attrName] != null)
            {
                return node.Attributes[attrName].InnerXml;
            }

            return null;
        }

        #region IConfigurationSectionHandler Members

        public object Create(object parent, object configContext, XmlNode section)
        {
            ServiceFactoryConfiguration config = new ServiceFactoryConfiguration();

            string type = GetAttributeValue(section, "type");
            if (string.IsNullOrEmpty(type))
            {
                type = "Local";
            }
            config.Type = (type.ToLower() == "remoting" ? ServiceFactoryType.Remoting : ServiceFactoryType.Local);
            if (config.Type == ServiceFactoryType.Remoting)
            {
                config.Protocol = (GetAttributeValue(section, "protocol").ToLower() == "tcp" ? RemotingChannelType.TCP : RemotingChannelType.HTTP);
                config.Server = GetAttributeValue(section, "server");
                try
                {
                    if (GetAttributeValue(section, "port") != null)
                    {
                        config.Port = int.Parse(GetAttributeValue(section, "port"));
                    }
                }
                catch
                {
                }
                try
                {
                    if (GetAttributeValue(section, "maxTry") != null)
                    {
                        config.MaxTry = int.Parse(GetAttributeValue(section, "maxTry"));
                    }
                }
                catch
                {
                }
                if (GetAttributeValue(section, "name") != null)
                {
                    config.ServiceMQName = GetAttributeValue(section, "name");
                }
                if (GetAttributeValue(section, "debug") != null)
                {
                    if (GetAttributeValue(section, "debug").ToLower() == "false")
                    {
                        config.Debug = false;
                    }
                }
            }
            
            return config;
        }

        #endregion
    }


    public class ServiceFactoryConfiguration
    {
        public ServiceFactoryType Type = ServiceFactoryType.Local;
        public RemotingChannelType Protocol = RemotingChannelType.HTTP;
        public string Server = "127.0.0.1";
        public int Port = 8888;
        public string ServiceMQName = "MMQ";
        public bool Debug = true;
        public int MaxTry = SimpleServiceContainer.DEFAULT_MAX_TRY_NUMBER;
    }

    public enum ServiceFactoryType
    {
        Local,
        Remoting
    }
}
